package com.project.base.controller;

import com.project.bean.file.SaveFileOutDTO;
import com.project.cache.helper.CacheProxy;
import com.project.core.anno.ScoreLog;
import com.project.core.entity.User;
import com.project.core.util.ScoreContext;
import com.project.file.helper.FilePropertiesHelper;
import com.project.file.helper.FileProxy;
import com.project.queue.helper.QueueProxy;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author mengxj
 */
@Api("测试")
@RestController
@RequestMapping("test")
public class TestController {

    private CacheProxy baseCache = new CacheProxy();

    private FileProxy baseFile = new FileProxy();

    private QueueProxy baseQueue = new QueueProxy();

    private String testKey = "test::key";

    @ApiOperation("获取当前人信息")
    @GetMapping("current")
    public User current(){
        return ScoreContext.currentUser();
    }

    @ApiOperation("不传参数获取当前人信息")
    @GetMapping("user")
    public User user(@ApiIgnore User user){
        return user;
    }

    @ScoreLog("插入缓存")
    @ApiOperation("插入缓存")
    @GetMapping("set-cache")
    public void setCache(@RequestParam String value){
        this.baseCache.set(this.testKey, value);
    }

    @ApiOperation("获取缓存")
    @GetMapping("get-cache")
    public Object getCache(){
        return this.baseCache.getValue(this.testKey);
    }

    @ApiOperation("删除缓存")
    @DeleteMapping("remove-cache")
    public void removeCache(){
        this.baseCache.remove(this.testKey);
    }

    @ApiOperation("保存文件")
    @GetMapping("save-file")
    public SaveFileOutDTO saveFile(@RequestParam("file") MultipartFile file){
        return this.baseFile.saveFile(file);
    }

    @ApiOperation("下载文件")
    @GetMapping("download-file")
    public void downloadFile(){
        String bucket = FilePropertiesHelper.bucket();
        String objectName = "2022-12-02/jpeg/1669965819871/tlp.jpeg";
        String fileName = "aaa.jpeg";
        this.baseFile.download(bucket, objectName, fileName);
    }

    @ApiOperation("删除文件")
    @GetMapping("remove-file")
    public void removeFile(){
        String bucket = FilePropertiesHelper.bucket();
        String objectName = "2022-12-02/jpeg/1669965819871/tlp.jpeg";
        this.baseFile.removeFile(bucket, objectName);
    }

    @ApiOperation("向队列（kafka/rabbitMq，由配置文件决定）发送消息")
    @GetMapping("send")
    public void sendMessage(@RequestParam String queue, @RequestParam String value){
        this.baseQueue.send(queue, value);
    }
}
